﻿using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;
using DHRSoftphone.AgentExceptions;

namespace DHRSoftphone.SoftphoneAgent_QZJ
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            bool createdNew;
            Mutex instance = new Mutex(true, "DHR_Softphone_Agent", out createdNew);

            if (createdNew)
            {
                Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionFunction);

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                Application.Run(new FormMain());

                //Application.Run(new FrmMeetingRoom());
                //Application.Run(new FrmMeetingRoom());
                instance.ReleaseMutex();
            }

            else
            {
                //MessageBox.Show("This program has already been started.", "DHR Softphone Agent", MessageBoxButtons.OK,
                //MessageBoxIcon.Error);
                Application.Exit();
            }
        }

        private static void UnhandledExceptionFunction(Object sender, UnhandledExceptionEventArgs args)
        {
            Exception e = (Exception)args.ExceptionObject;
            new AgentException("Application_ThreadException", e);
            string tempDir = System.Environment.GetEnvironmentVariable("TEMP");
            string filename = DateTime.Now.ToString("yyyyMMddhhmmss_" + new Random().NextDouble().ToString().Replace(".", string.Empty) + ".txt");
            string finalname = Path.Combine(tempDir, filename);

            TextWriter tw = new StreamWriter(finalname);
            tw.WriteLine("This content below was generated by Direct HR Softphone Agent Reporter");
            tw.WriteLine("Program met some problems, please send the message below to Vincent Qiu v.qiu@directhr.cn. Thank you.");
            tw.WriteLine("Program Version:" + Assembly.GetExecutingAssembly().GetName().Version);
            tw.WriteLine("UnhandledExceptionFunction");
            tw.WriteLine(e.Message);
            tw.WriteLine(e.StackTrace);

            if (e.InnerException != null)
            {
                tw.WriteLine(e.InnerException.Message);
                tw.WriteLine(e.InnerException.StackTrace);
            }

            tw.Close();
            System.Diagnostics.Process.Start("NotePad.exe ", finalname);
            Application.Exit();
            Environment.Exit(0);
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            new AgentException("Application_ThreadException", e.Exception);
            string tempDir = System.Environment.GetEnvironmentVariable("TEMP");
            string filename = DateTime.Now.ToString("yyyyMMddhhmmss_" + new Random().NextDouble().ToString().Replace(".", string.Empty) + ".txt");
            string finalname = Path.Combine(tempDir, filename);

            TextWriter tw = new StreamWriter(finalname);
            tw.WriteLine("This content below was generated by Direct HR Softphone Agent Reporter");
            tw.WriteLine("Program met some problems, please send the message below to Vincent Qiu v.qiu@directhr.cn. Thank you.");
            tw.WriteLine("Program Version:" + Assembly.GetExecutingAssembly().GetName().Version);
            tw.WriteLine("ThreadException");
            tw.WriteLine(e.Exception.Message);
            tw.WriteLine(e.Exception.StackTrace);

            if (e.Exception.InnerException != null)
            {
                tw.WriteLine(e.Exception.InnerException.Message);
                tw.WriteLine(e.Exception.InnerException.StackTrace);
            }

            tw.Close();
            System.Diagnostics.Process.Start("NotePad.exe ", finalname);
            Application.Exit();
            Environment.Exit(0);
        }
    }
}